package com.kurumi.autoprint.service.impl.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kurumi.autoprint.bo.PageParam;
import com.kurumi.autoprint.mapper.AdminUserMapper;
import com.kurumi.autoprint.pojo.admin.AdminUser;
import com.kurumi.autoprint.service.admin.AdminUserServcie;
import com.kurumi.autoprint.util.MD5;

@Service
public class AdminUserServiceImpl implements AdminUserServcie {

	@Autowired
	private AdminUserMapper adminUserMapper;

	@Transactional(readOnly = true)
	@Override
	public AdminUser getUserByUsernameAndPassword(String username, String password) {
		AdminUser adminUser = adminUserMapper.getUserByUsernameAndPassword(username, MD5.md5(password));
		return adminUser;
	}

	@Transactional(readOnly = true)
	@Override
	public List<AdminUser> getUserByUsername(String username) {
		return adminUserMapper.getUserByUsername(username);
	}

	@Transactional(readOnly = true)
	@Override
	public PageInfo<AdminUser> geAdminUserPageInfo(HttpServletRequest request, PageParam pageParam) {
		PageHelper.startPage(pageParam.getPage(), pageParam.getLimit());
		Map<String,Object> params = new HashMap<>();
		
		String name = request.getParameter("name");
		if(StringUtils.isNotEmpty(name)) {
			params.put("name", name);
		}
		
		String idCard = request.getParameter("idCard");
		if(StringUtils.isNotEmpty(idCard)) {
			params.put("idCard", idCard);
		}
		
		String userName = request.getParameter("userName");
		if(StringUtils.isNotEmpty(userName)) {
			params.put("userName", userName);
		}
		List<AdminUser> adminUserList =  adminUserMapper.getAllAdminUser(params);
		PageInfo<AdminUser> objectPageInfo = new PageInfo<>(adminUserList);
		return objectPageInfo;

	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void saveOrUpdateAdminUser(AdminUser adminUser) {
		Integer id = adminUser.getId();
		if(id==null) {
			adminUser.setStatus(1);
			adminUser.setRecordTime(new Date());
			String password= adminUser.getPassword();
			adminUser.setPassword(MD5.md5(password));
			adminUserMapper.insertSelective(adminUser);
		}else {
			adminUserMapper.updateByPrimaryKey(adminUser);
		}
		
		
		
	}

	@Transactional(readOnly = true)
	@Override
	public AdminUser getAdminUserById(Integer adminUserId) {
		return adminUserMapper.selectByPrimaryKey(adminUserId);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void delAdminUserById(Integer id) {
		
		adminUserMapper.deleteByPrimaryKey(id);
	}

	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public AdminUser updateAdminUserPassword(Integer adminUserId, String newPassword) {
		AdminUser adminUser = adminUserMapper.selectByPrimaryKey(adminUserId);
		adminUser.setPassword(MD5.md5(newPassword));
		adminUserMapper.updateByPrimaryKey(adminUser);
		
		return adminUser;
		
	}

}
